﻿using Xejen.Ipc.Framework.Flows;
using System;
using System.Collections.Generic;
using System.Text;

namespace Xejen.Ipc.Framework.Steps
{
    /// <inheritdoc cref="IStepBuilder{TData}"/>
    /// <typeparam name="TData"><inheritdoc cref="IFlow{TData}" path="/typeparam[@name='TData']"/></typeparam>
    /// <typeparam name="TStepBody"><inheritdoc cref="IStepBody{TData}" path="/summary"/></typeparam>
    internal class StepBuilder<TData, TStepBody> : IStepBuilder<TData>
        where TData : new()
        where TStepBody : IStepBody<TData>, new()
    {
        private readonly string _stepName;

        /// <inheritdoc cref="StepBuilder{TData, TStepBody}"/>
        /// <param name="stepName"><inheritdoc cref="IStep{TData}.Name" path="/summary"/></param>
        public StepBuilder(string stepName)
        {
            _stepName = stepName;
        }

        /// <inheritdoc/>
        public IStep<TData> Build()
        {
            IdGenerator generator = new IdGenerator();
            var id = generator.GetId();
            Step<TData, TStepBody> step = new Step<TData, TStepBody>(id, _stepName);

            return step;
        }
    }

    /// <summary>
    /// 用于生成步骤id
    /// </summary>
    internal sealed class IdGenerator
    {
        private static int _stepCount = 0;

        /// <inheritdoc cref="IdGenerator"/>
        public IdGenerator() { }

        /// <summary>
        /// 取自增id
        /// </summary>
        /// <returns></returns>
        public int GetId()
        {
            _stepCount++;

            return _stepCount;
        }
    }
}
